package com.cslg.ac.demo.controller.search;


import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.service.search.SearchService;
import com.cslg.ac.demo.service.user.TeamInfoService;
import com.cslg.ac.demo.util.communicate.EmailUtil;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import com.cslg.ac.demo.util.integer.IntegerUtil;
import com.cslg.ac.demo.util.string.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.aspectj.apache.bcel.generic.LineNumberGen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@PropertySource(value = {"classpath:user.properties"},encoding="utf-8")
public class SearchController {
    @Autowired
    SearchService searchService;
    @Autowired
    TeamInfoService teamInfoService;
    @Value("${nameLength}")
    int nameLength;
    @Value("${phoneLength}")
    int phoneLength;


    @RequestMapping(value = "/search/petSortByFive")
    public void getPostByType(@RequestParam(required = false, defaultValue = "null")String address, @RequestParam(required = false, defaultValue = "-1")Integer gender,
                              @RequestParam int offset, @RequestParam int num, @RequestParam(required = false, defaultValue = "null") String variety,
                              @RequestParam(required = false, defaultValue = "null") String longitude, @RequestParam(required = false, defaultValue = "null") String latitude,
                              PrintWriter out){

        boolean flag = (offset < 1 || num < 0);
        if(flag){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        List<Integer> ids;
        Map<String,Object> param = new HashMap<>();
        try {
            ids = searchService.petsByThree( offset - 1, num, address, gender, variety);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
            return;
        }
        if(ids == null || ids.size() < 1){
            ids = null;
        }
        param.put("ids",ids);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }

    @RequestMapping(value = "/search/postsByType")
    public void getPostByType(@RequestParam int type, @RequestParam int postType,
                              @RequestParam int offset, @RequestParam int num,
                              @RequestParam(required = false, defaultValue = "0") int chooseType,
                              HttpSession session ,PrintWriter out){
        User user = (User)session.getAttribute("user");

        boolean flag = (offset < 1 || num < 0) || (!IntegerUtil.valid(chooseType, 0 , 1));
        if(flag){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        Integer[] ids;
        Map<String,Object> param = new HashMap<>();
        try {
            ids = searchService.postByType(postType, type, offset - 1, num,chooseType,user.getId());
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
            return;
        }
        if(ids == null || ids.length < 1){
            ids = null;
        }
        param.put("ids",ids);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }

    @RequestMapping(value = "/search/usersByAddress",method = RequestMethod.POST)
    public void getIdsByAddress(@RequestParam String address, @RequestParam int userType,
                                @RequestParam int offset, @RequestParam int num,
                                HttpSession session, PrintWriter out){
        if(offset < 1 || num < 0){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        Integer[] ids;
        try {
            ids = searchService.usersByAdress(address,userType,offset - 1,num);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        if(ids == null || ids.length < 1){
            ids = null;
        }
        param.put("ids",ids);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }


    @RequestMapping(value = "/search/userByPhone",method = RequestMethod.POST)
    public void getIdByPhone(@RequestParam String phone, PrintWriter out, HttpSession session){
        if(!StringUtil.allNumber(phone) || phone.length() != phoneLength){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        Integer id;
        try {
            id = searchService.userByPhone(phone);
        } catch (Exception e){
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.USER_NOT_EXISTS);
            return;
        }
        if(id == null){
            ResponseUtil.responseCode(out,ResponseCode.USER_NOT_EXISTS);
            return;
        }
        Map<String,Object> param = new HashMap<>(1);
        param.put("id",id);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }

    @RequestMapping(value = "/search/usersByName",method = RequestMethod.POST)
    public void getPhoneByName(@RequestParam String name, @RequestParam int userType,
                               @RequestParam int offset, @RequestParam int num,
                               PrintWriter out, HttpSession session){
        if(StringUtil.haveNullOrEmpty(name) || StringUtil.lengthAbove(name,nameLength)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        if(offset < 1 || num < 0){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        Integer[] ids;
        try {
            ids = searchService.usersByName(name,userType,offset - 1,num);
        }  catch (Exception e){
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        if(ids == null || ids.length < 1){
            ids = null;
        }
        Map<String,Object> param = new HashMap<>(1);
        param.put("ids",ids);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);

    }

    @RequestMapping(value = "/search/userByEmail",method = RequestMethod.POST)
    public void getPhoneByName(@RequestParam String email, HttpSession session, PrintWriter out){
        if(!EmailUtil.isMailAddress(email)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        Integer id;
        try {
            id = searchService.userbyEmail(email);

        } catch (Exception e){
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.USER_NOT_EXISTS);
            return;
        }
        if(id == null){
            ResponseUtil.responseCode(out,ResponseCode.USER_NOT_EXISTS);
            return;
        }
        Map<String,Object> param = new HashMap<>(1);
        param.put("id",id);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }

}
